﻿/// <reference path="jquery-1.4.4-vsdoc.js" />



(function ($) {
    $.fn.pwdStrength = function (options) {

        var defaults = {
            target: null,
            minLength: 0,
            cssClass: 'pwd-strength',
            showCaption: true,
            captions: ['Weak', 'Normal', 'Medium', 'Strong', 'Very Strong'],
            meta: null, // not use
            onCheck: null
        };

        //var settings = $.extend(defaults, options);

        function getPasswordStrength(password, minLength) {
            if (password.length < minLength) return 0;

            var level = 0;

            // if password bigger than 5 give 1 point
            if (password.length > 6) level++;
            // if password has both lower and uppercase characters give 1 point
            if (/[a-z]/.test(password) && /[A-Z]/.test(password)) level++;
            // if password has at least one number and at least 1 other character give 1 point
            if (/\d/.test(password) && /\D/.test(password)) level++;
            // if password has a combination of other characters and special characters give 1 point
            if (/[!,@,#,$,%,^,&,*,?,_,~,-,(,),]/.test(password) && /\w/.test(password)) level++;
            // if password bigger than 12 give another 1 point (thanks reddit)
            if (password.length > 12) level++;

            return level;
        }

        return this.each(function () {

            var _this = $(this);

            /*
            var meta = this.settings.meta
            ? $(element).metadata()[this.settings.meta]
            : $(element).metadata();
            */

            var settings = $.extend(defaults, options || {}, $.metadata ? _this.metadata() : $.meta ? _this.data() : {});

            var _container;

            var _ps = $('<div/>').attr("id", 'pwd_strength_' + _this.attr("id")).attr('class', settings.cssClass);
            var _caption = $('<span class="pwd-strength-label" />');

            if (settings.target) {
                _container = $(settings.target);
                _container.append(_ps);
                _container.append(_caption);
            }
            else {
                _container = $('<div/>').width(_this.width());
                _container.append(_ps);
                _container.append(_caption);
                _this.after(_container);

            };

            _this.bind('keyup blur', function () {
                var val = $(this).val();
                var level = 0;
                if (val.length > 0) {

                    level = getPasswordStrength(val, settings.minLength);
                    var _class = settings.cssClass + '-' + level;

                    if (!_ps.hasClass(_class)) {
                        _ps.attr('class', settings.cssClass + ' ' + _class).show(); //.css({'display': 'block', 'background-color': digitalspaghetti.password.strColor, 'width': digitalspaghetti.password.width + "%"});

                        if (settings.showCaption) {
                            _caption.text(settings.captions[level]).show();
                        }

                    }
                }
                else {
                    _ps.attr('class', settings.cssClass).hide();
                    _caption.hide();
                };
                if (settings.onCheck) {
                    settings.onCheck.call(this, level);
                }

            });


        });
    };
})(jQuery);
